const Mock=require('mockjs')
const express=require('express')
const app=express()

// 根据传入的参数num，生成num条模拟的数据列表
function generatorList(num) {
    return Mock.mock({
        [`list|${num}`]:[{
            'id|+1':1,// 模拟ID,自增方式增加
            title:"@ctitle(15,25)",//title
            image:"@natural(0,15)",//模拟图片
            reads:"@natural(0,99999)",
            from:"@ctitle(3.10)",
            date:"@date('yyyy-MM-dd')"
        }]
    });
}

// 允许跨域请求返回数据
app.all('*',function(req,res,next) {
    res.header('Access-Control-Allow-Origin','*');
    res.header('Access-Control-Allow-Methods','PTU,GET,POST,DELETE,OPTIONS');
    res.header('Access-Control-Headers','X-Requested-With');
    res.header('Access-Control-Allow-Headers','Content-Type');
    next();
});

app.get('/data',function(req,res){
    //获取get请求数据条数参数num
    const{num} =req.query;
    return res.send(generatorList(num));

})
// 设置端口并打印对应调用结果
const server=app.listen(4000,function(){
    console.log('本地mock启动成功,接口地址为：http://localhost:4000/data?num=请求列表数量');
    
})